package org.application.calculator.interf;

import java.io.*;
import java.util.Scanner;

import org.application.calculator.logic.DenominatorIsZeroException;
import org.application.calculator.logic.LineProcessor;

public class CalculatorApplication {

private static String expression;
	
	public static String getScannedExpression(){
		return expression;
	}
	
	public static String ReadFromStandardInputStream(){
		
		Scanner in = new Scanner(System.in);
		System.out.println("Enter expression: ");
		String line = in.next();
		in.close();
		
		return line;
	}
	
	public static String ReadFromFile(String pathToFile) throws IOException{
		
		BufferedReader reader = null;
	    
	        reader = new BufferedReader(new FileReader(pathToFile));
	        String line = reader.readLine();
	    	reader.close();
		
		return line;
	}
	
	public static void WriteIntoFile(String pathToFile, String expression, double evaluate) throws IOException{
		
            PrintWriter out = new PrintWriter(new File(pathToFile).getAbsoluteFile());
                out.print(expression + " = " + evaluate);
                out.close();
	}

	public static void main(String[] args){
		
		System.out.println("Starting Calculator\nVersion 1.0");
		
			if (args.length == 0) {
				expression = ReadFromStandardInputStream();
				try {
					System.out.println(LineProcessor.createOperators(expression).toString() + 
							" = " + LineProcessor.createOperators(expression).evaluate());
				} catch (DenominatorIsZeroException e) {
					System.out.println("Division by 0");
				}
				System.out.println();
			
			} else {
				
					
					if (args.length == 2){
					
						if (args[0].equals("in")) {
							try {
								expression = ReadFromFile(args[1]);
								try {
									System.out.println(LineProcessor.createOperators(expression).toString() + 
											" = " + LineProcessor.createOperators(expression).evaluate());
								} catch (DenominatorIsZeroException e) {
									System.out.println("Division by 0");
								}
							} catch (IOException e) {
								System.out.println("The path to input file is not correct");
							}
						} else {
					
						
						if (args[0].equals("out")) {
							expression = ReadFromStandardInputStream();
							try {
								try {
									WriteIntoFile(args[1],LineProcessor.createOperators(expression).toString(), 
											LineProcessor.createOperators(expression).evaluate());
								} catch (DenominatorIsZeroException e) {
									System.out.println("Division by 0");
								}
							} catch (IOException e) {
								e.printStackTrace();
							}
						} else {
							System.out.println("The key is not correct");
						}
						}
					
					
					} else {
					
						
						if (args.length == 4 && args[0].equals("in") && args[2].equals("out")) {
							try {
								expression = ReadFromFile(args[1]);
							} catch (IOException e) {
								System.out.println("The path input to file is not correct");
							}
							try {
								try {
									WriteIntoFile(args[1],LineProcessor.createOperators(expression).toString(), 
											LineProcessor.createOperators(expression).evaluate());
								} catch (DenominatorIsZeroException e) {
									System.out.println("Division by 0");
								}
							} catch (IOException e) {
								e.printStackTrace();
							}
						} else {
							System.out.println("Too much arguments or keys are not correct");
						}
				}
				}
	}
}
